package pages;

import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import utils.Log4Test;

import java.util.List;

public class ApplePage extends GeneralPage {

    private String URL = "http://rozetka.com.ua/notebooks/apple/c80004/v069/";
    protected By Comparebut = By.xpath("(//a[@href='http://rozetka.com.ua/computers-notebooks/notebooks/comparison/ids=699984%2C338650/'])[2]");
    protected By MacBookPlace = By.xpath("//div[1]/div[3]/div/div[1]/div[2]/div/div[4]/div[1]/div[1]/div");
    protected By CompareList=By.xpath("//div[1]/div[3]/div/div[1]/div[1]/div[1]/div/div/div/ul/li/a");
    protected List<WebElement> AppleMacBookList;



    protected WebElement elementIsLocated(By element)

    {
        try {
            return wait.until(ExpectedConditions.presenceOfElementLocated(element));
        } catch (StaleElementReferenceException e) {
            return wait.until(ExpectedConditions.presenceOfElementLocated(element));
        } catch (NoSuchElementException ele) {
            return wait.until(ExpectedConditions.presenceOfElementLocated(element));
        }
    }


    //7. Verify that the page is Ноутбуки Apple
    public boolean Opened() {
        Log4Test.info("Open ApplePage");
        return webDriver.getCurrentUrl().equals(URL);  //Return true if we opened AppleTab, else - false
    }

    //8. Click "Выводить: от дорогих к дешевым"
    public void ClickTab() {
        Log4Test.info("ClickTab");
        webDriver.findElement(By.xpath("//div[@id='sort_view']/a/i")).click();
    }

    public void ClickSortFromExpansiveToCheap() {
        Log4Test.info("ClickSortFrom ExpansiveToCheap");
        webDriver.findElement(By.linkText("от дорогих к дешевым")).click();
    }


    // 9. Verify that there is Apple MacBook Pro Retina 15" (Z0PU002JE)
    public boolean ExistMacBookFirst() {
        Log4Test.info("ExistApple MacBook Pro Retina 15 (Z0PU002JE)");
        return webDriver.getPageSource().contains("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!");
    }

    // 10. Click boolean check "К сравнению" for Apple MacBook Pro Retina 15" (Z0PU002JE)

    public void AppleMacBookFirstClick() {

        for (WebElement Elem : AppleMacBookList = webDriver.findElements(MacBookPlace)) {
            if (Elem.getText().contains("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!")) {
                Elem.findElement(By.name("tocomparison")).click();
                Log4Test.info("Apple MacBook Pro Retina 15\" (Z0PU002JE) ");
            }
        }
    }

    //11. On the right side of the page should appear "Cписок сравнения:" that contains Apple MacBook Pro Retina 15" (Z0PU002JE) Официальная гарантия!(you should verify it :) )
    public String AppearListofComparisionFirst() {
        Log4Test.info("In Comparision block contains Apple MacBook Pro Retina 15\" (Z0PU002JE)");
        return webDriver.findElement(By.className("list-compare")).getText();

    }

    //12.Verify that there is Apple MacBook Pro Retina 15" (ME294UA/A)
    public boolean ExistMacBookSecond() {
        Log4Test.info("Exist Apple MacBook Pro Retina 15 (ME294UA/A)");
        return webDriver.getPageSource().contains("Apple MacBook Pro Retina 15\" (ME294UA/A)");
    }

    // 13.Click boolean check "К сравнению" for Apple MacBook Pro Retina 15" (ME294UA/A)

    public void AppleMacBookSecondClick() {

        for (WebElement Elem : AppleMacBookList = webDriver.findElements(MacBookPlace)) {
            if (Elem.getText().contains("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")) {
                Elem.findElement(By.name("tocomparison")).click();
                Log4Test.info("Click To comparision Apple MacBook Pro Retina 15\" (ME294UA/A)");
            }
        }
    }

    // 14 On the right side of the page should appear "Cписок сравнения:"  that contains  Apple MacBook Pro Retina 15" (Z0PU002JE) Официальная гарантия! and
    // Apple MacBook Pro Retina 15" (ME294UA/A) Официальная гарантия! (you should verify it :)

    public boolean AppearListofComparisionTwoMacBooks() {

        List<WebElement> ComparisionList = webDriver.findElements(CompareList);
        if (ComparisionList.get(1).getText().contains("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!") && ComparisionList.get(3).getText().contains("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")) {
            Log4Test.info("Cписок сравнения contains Apple MacBook Pro Retina 15\" (Z0PU002JE), Apple MacBook Pro Retina 15\" (ME294UA/A))");
            return true;
        } else return false;
    }


    //15.Click "В сравнении" from one of the product's name above

    public void ClickToCompareButton() {
        Log4Test.info("Click To Compare Button");
        webDriver.findElement(Comparebut).click();
    }

    //This method doeasn't work!!!!
        /*public void ClickToCompareButton() {
        for (WebElement Elem : AppleMacBookList = webDriver.findElements(MacBookPlace)) {
         if (Elem.getText().contains("Apple MacBook Pro Retina 15\" (ME294UA/A)"))
        {
            Elem.findElement(By.linkText("В сравнении")).click();
            Log4Test.info("ClickToCompareButton");
        }
       }
          }*/


}






